//
//  MyViewController.m
//  inquiry
//
//  Created by 高宇 on 2020/5/23.
//  Copyright © 2020 高宇. All rights reserved.
//

#define SCREEN_WIDTH                 ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT                ([[UIScreen mainScreen] bounds].size.height)

#import "MyViewController.h"
#import "XTAlertViewController.h"
#import "PersonalViewController.h"
#import "ULNetHelper.h"
#import "Interface.h"
#import "ConfigViewController.h"

@interface MyViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *downImageView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *userImageView;
@property (weak, nonatomic) IBOutlet UILabel *userNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *yuanLabel;

@end

@implementation MyViewController

//在页面出现的时候就将黑线隐藏起来
 -(void)viewWillAppear:(BOOL)animated
{
    //隐藏导航栏分割线
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self getData]; //更新学号
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *studentName = [user objectForKey:@"studentName"];
    self.userNameLabel.text = studentName;
    if (studentName.length>2) {
        self.yuanLabel.text = [studentName substringFromIndex:studentName.length-2];
    }else{
        self.yuanLabel.text = studentName;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];

    //创建UI
    [self createUI];
}

- (void)getData{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"uuid"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"uuid"];
    
    [ULNetHelper POST:queryStudentIdFormUserIdPath parameters:dic success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        long code = [dic[@"code"] longValue];
        if (code == 200) {
            self.userNumLabel.text = [NSString stringWithFormat:@"学号：%@",dic[@"data"]];
        }else{
            
        }
    }];
}

- (void)createUI{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *studentName = [user objectForKey:@"studentName"];
    self.userNameLabel.text = studentName;
    if (studentName.length>2) {
        self.yuanLabel.text = [studentName substringFromIndex:studentName.length-2];
    }else{
        self.yuanLabel.text = studentName;
    }
    
    
    self.yuanLabel.layer.borderWidth = 2;
    self.yuanLabel.layer.borderColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
    self.yuanLabel.layer.masksToBounds = YES;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, CGRectGetMaxY(self.downImageView.frame)+10, SCREEN_WIDTH, 48);
    button.backgroundColor = [UIColor whiteColor];
    [button addTarget:self action:@selector(pwClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"矩形(3)"]];
    imageView.frame = CGRectMake(28, 12, 24, 24);
    [button addSubview:imageView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+10,CGRectGetMinY(imageView.frame), CGRectGetMaxX(button.frame)-120, imageView.frame.size.height)];
    label.text = @"修改密码";
    label.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
    [button addSubview:label];
    
    UIImageView *pushImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"矩形(4)"]];
    pushImageView.frame = CGRectMake(CGRectGetMaxX(button.frame)-44, 12, 24, 24);
    [button addSubview:pushImageView];
    
    //第二行
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(0, CGRectGetMaxY(button.frame)+10, SCREEN_WIDTH, 48);
    button2.backgroundColor = [UIColor whiteColor];
    [button2 addTarget:self action:@selector(informationClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"矩形-3"]];
    imageView2.frame = CGRectMake(28, 12, 24, 24);
    [button2 addSubview:imageView2];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView2.frame)+10,CGRectGetMinY(imageView2.frame), CGRectGetMaxX(button2.frame)-120, imageView2.frame.size.height)];
    label2.text = @"个人资料";
    label2.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
    [button2 addSubview:label2];
    
    UIImageView *pushImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"矩形(4)"]];
    pushImageView2.frame = CGRectMake(CGRectGetMaxX(button2.frame)-44, 12, 24, 24);
    [button2 addSubview:pushImageView2];
    
    //第二行
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake(0, CGRectGetMaxY(button2.frame)+10, SCREEN_WIDTH, 48);
    button3.backgroundColor = [UIColor whiteColor];
    [button3 addTarget:self action:@selector(shezhiClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    
    UIImageView *imageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"矩形-4"]];
    imageView3.frame = CGRectMake(28, 12, 24, 24);
    [button3 addSubview:imageView3];
    
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView3.frame)+10, 12, CGRectGetMaxX(button2.frame)-20, imageView2.frame.size.height)];
    label3.text = @"设置";
    label3.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
    [button3 addSubview:label3];
    
    UIImageView *pushImageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"矩形(4)"]];
    pushImageView3.frame = CGRectMake(CGRectGetMaxX(button2.frame)-44, 12, 24, 24);
    [button3 addSubview:pushImageView3];
}

- (void)shezhiClick{
    ConfigViewController *vc = [[ConfigViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

//修改密码
- (void)pwClick:(UIButton *)button{
    XTAlertViewController *vc = [XTAlertViewController alertWithTitle:nil andContent:nil];
    vc.hidesBottomBarWhenPushed = YES;
    [self.view addSubview:vc.view];
    [self addChildViewController:vc];
    
    vc.block = ^{
        [vc.view removeFromSuperview];
        [vc removeFromParentViewController];
    };
    
    
}

- (void)informationClick:(UIButton *)button{
    PersonalViewController *vc = [[PersonalViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
